<?php
/**
 * Created by PhpStorm.
 * User: Ghost
 * Date: 2017/11/24
 * Time: 17:00
 */

namespace app\admin\model;


class Orders
{
    public function edit($data, $khd = 1)
    {
        if (!empty($data['id'])) {
            $wxts = Db::name('order')->where('id', $data['id'])->field(['wxts', 'state', 'zjsid'])->find();
            $re = $this->allowField(true)->validate('orders')->isUpdate()->save($data);
            if ($re) {
                $order_log = new Order_log;
                if (empty($data['state'])) {
                    $data['state'] = $wxts['state'];
                }
                $log = ['order_id' => $data['id'], 'operation' => 2, 'order_state' => $data['state'], 'yykf' => $data['yykf'], 'menshi' => $data['menshi'], 'mendian' => $data['mendian'], 'qdbz' => $data['qdbz'], 'yybz' => $data['yybz'], 'msbz' => $data['msbz'], 'khd' => $khd, 'name' => $data['name'], 'phone' => $data['phone'], 'weixin' => $data['weixin'], 'qq' => $data['qq']];
                $order_log->log($log, $wxts);//日志
                if (($data['state'] == 2) && ($wxts['wxts'] == 1)) {
                    Weixin::weinxinapi($data['id']);
                    $orderwx['wxts'] = 2;
                    $orderwx['id'] = $data['id'];
                    $this->isUpdate()->save($orderwx);

                } elseif (($data['state'] == 6 || $data['state'] == 3)) {
                    Weixin::weixinchange($data['id']);
                } else {
                    self::order_bg($data);
                }
                if($data['state'] == 6 || $data['state'] == 11){
                    sendBaocai(['type'=>1,'msg'=>$data['id']]);
                }
                //分页
                $session_p = session('map');
                if (empty($session_p['p'])) {
                    return ['status' => '1', 'info' => '数据更新成功！', 'url' => url('olist', ['editid' => $data['id']])];
                } else {
                    return ['status' => '1', 'info' => '数据更新成功！', 'url' => url('olist', ['editid' => $data['id'], 'p' => $session_p['p']])];
                }
                //action_log('updateGroup', 'Group', $depar->id, UID);//日志
            } else {
                return ['status' => '0', 'info' => $this->getError(), 'url' => url('edit', ['id' => $data['id']])];
            }
        } else {
            //电话转化建单时间
            if ($data['state'] == 9) {
                $data['dhzhtime'] = time();
            }
            $re = $this->allowField(true)->validate(true)->save($data);
            if ($re) {
                $order_log = new Order_log;
                $log = ['order_id' => $this->id, 'operation' => 1, 'order_state' => $data['state'], 'yykf' => $data['yykf'], 'menshi' => $data['menshi'], 'mendian' => $data['mendian'], 'qdbz' => $data['qdbz'], 'yybz' => $data['yybz'], 'msbz' => $data['msbz'], 'khd' => 1, 'name' => $data['name'], 'phone' => $data['phone'], 'weixin' => $data['weixin'], 'qq' => $data['qq']];
                $order_log->log($log); //日志

                if ($data['state'] == 2) {
                    Weixin::weinxinapi($this->id);
                } elseif ($data['state'] == 6 || $data['state'] == 3) {
                    Weixin::weixinchange($this->id);
                }
                //action_log('addGroup', 'Group', $depar->id, UID); //日志
                return ['status' => '1', 'info' => '新增成功！', 'url' => url('olist')];
            } else {
//                         return ['status'=>'1','info'=>'新增失败！'];
                return ['status' => '0', 'info' => $this->getError(), 'url' => url('edit', ['id' => $data['id']])];
            }
        }
    }
}